// components/merchantType/merchantType.js
import { request } from '../../utils/requestUtils';
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    currentValue: {
      type: String,
      value: ""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    value: "",
    columns: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    merchantChange(event) {
      const { picker, value, index } = event.detail;
      this.setData({
        value: value
      })
    },
    //商家取消选择
    merchantCancel() {
      console.log('取消选择')
      this.triggerEvent('merchantCancel')
    },
    //选择商家
    merchantConfirm() {
      if (!this.data.value) {
        wx.showToast({
          title: '请选择商家类型',
          icon: 'error',
          duration: 2000,
          mask: true
        })
        return
      }
      this.triggerEvent('merchantConfirm', this.data.value)
    },
    //查询商家类型别
    async getMerchantType() {
      const result = await request({
        url: '/system/dict/data/type/merchant_type',
        method: "GET"
      })
      //设置默认值
      if (this.properties.currentValue && result.data.data && result.data.data.length > 0) {
        const current = result.data.data.find(v => v.dictValue == this.properties.currentValue);
        const currentIndex = result.data.data.findIndex(v => v.dictValue == this.properties.currentValue);
        console.log('currentIndex', currentIndex)

        this.setData({
          value: current
        })
      }
      console.log('result', result)
      this.setData({
        columns: result.data.data
      })
    }
  },
  attached: function () {
    this.getMerchantType()
  },
})
